package com.ruoyi.web.controller.ora;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ora.domain.AssetsLike;
import com.ruoyi.ora.service.IAssetsLikeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 点赞Controller
 * 
 * @author liuwp
 * @date 2025-04-10
 */
@RestController
@RequestMapping("/ora/assets_like")
public class AssetsLikeController extends BaseController
{
    @Autowired
    private IAssetsLikeService assetsLikeService;
//    @Autowired
//    private IVoiceRecordService voiceRecordService;
//    @Autowired
//    private IVoiceMemoirService voiceMemoirService;
//    @Autowired
//    private IVoiceStoryService voiceStoryService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询点赞列表
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_like:list')")
    @GetMapping("/list")
    public TableDataInfo list(AssetsLike assetsLike)
    {
        startPage();
        List<AssetsLike> list = assetsLikeService.selectAssetsLikeList(assetsLike);
        return getDataTable(list);
    }

    /**
     * 导出点赞列表
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_like:export')")
    @Log(title = "点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssetsLike assetsLike)
    {
        List<AssetsLike> list = assetsLikeService.selectAssetsLikeList(assetsLike);
        ExcelUtil<AssetsLike> util = new ExcelUtil<AssetsLike>(AssetsLike.class);
        util.exportExcel(response, list, "点赞数据");
    }

    /**
     * 获取点赞详细信息
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_like:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(assetsLikeService.selectAssetsLikeById(id));
    }

    /**
     * 新增点赞
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_like:add')")
    @Log(title = "点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AssetsLike assetsLike)
    {
        return toAjax(assetsLikeService.insertAssetsLike(assetsLike));
    }

    /**
     * 修改点赞
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_like:edit')")
    @Log(title = "点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssetsLike assetsLike)
    {
        return toAjax(assetsLikeService.updateAssetsLike(assetsLike));
    }

    /**
     * 删除点赞
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_like:remove')")
    @Log(title = "点赞", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(assetsLikeService.deleteAssetsLikeByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('ora:assets_like:list')")
    @GetMapping("/query")
    public AjaxResult query(AssetsLike assetsLike)
    {
        List<AssetsLike> assetsLikes = assetsLikeService.selectAssetsLikeList(assetsLike);
        for (AssetsLike like : assetsLikes) {
            like.setSysUser(sysUserService.selectUserById(like.getUserId()));
        }
        return AjaxResult.success(assetsLikes);
    }

    @PreAuthorize("@ss.hasPermi('ora:assets_like:list')")
    @GetMapping("/islike")
    public AjaxResult islike(AssetsLike assetsLike)
    {
        assetsLike.setUserId(SecurityUtils.getUserId());
        return AjaxResult.success(assetsLikeService.selectOneAssetsLike(assetsLike));
    }

    @PreAuthorize("@ss.hasPermi('ora:assets_like:add')")
    @Log(title = "点赞", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addLike(@RequestBody AssetsLike assetsLike)
    {
//        assetsLike.setUserId(SecurityUtils.getUserId());
//
//        AssetsLike assetsLike1 = assetsLikeService.selectOneAssetsLike(assetsLike);
//        if (assetsLike1 == null){
//            if (assetsLike.getLikeType().intValue() == AssetsType.VOICE.getValue().intValue()){
//                VoiceRecord voiceRecord = voiceRecordService.selectVoiceRecordById(assetsLike.getAssetsId());
//                // 修改对应数据的赞数量
//                voiceRecordService.updateVoiceRecord(Builder.of(VoiceRecord::new)
//                        .with(VoiceRecord::setId, assetsLike.getAssetsId())
//                        .with(VoiceRecord::setLikeCount, voiceRecord.getLikeCount()==null?1:voiceRecord.getLikeCount() + 1)
//                        .build());
//            }
//            if (assetsLike.getLikeType().intValue() == AssetsType.MEMORI.getValue().intValue()){
//                VoiceMemoir voiceMemoir = voiceMemoirService.selectVoiceMemoirById(assetsLike.getAssetsId());
//                // 修改对应数据的赞数量
//                voiceMemoirService.updateVoiceMemoir(Builder.of(VoiceMemoir::new)
//                        .with(VoiceMemoir::setId, assetsLike.getAssetsId())
//                        .with(VoiceMemoir::setLikeCount, voiceMemoir.getLikeCount()==null?1:voiceMemoir.getLikeCount() + 1)
//                        .build());
//            }
//            if (assetsLike.getLikeType().intValue() == AssetsType.STROY.getValue().intValue()){
//                VoiceStory voiceStory = voiceStoryService.selectVoiceStoryById(assetsLike.getAssetsId());
//                // 修改对应数据的赞数量
//                voiceStoryService.updateVoiceStory(Builder.of(VoiceStory::new)
//                        .with(VoiceStory::setId, assetsLike.getAssetsId())
//                        .with(VoiceStory::setLikeCount, voiceStory.getLikeCount()==null?1:voiceStory.getLikeCount() + 1)
//                        .build());
//            }
//            return toAjax(assetsLikeService.insertAssetsLike(assetsLike));
//        }else {
//            return AjaxResult.success(assetsLike1);
//        }
        return AjaxResult.success();

    }
}
